package app

import (
	"crypto/md5"
	"fmt"
	"math/rand"
	"time"
)

// RandString 生成指定位数随机字符串
func RandString(n int) string {
	var letters = []rune("1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
	rand.Seed(time.Now().UnixNano())
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

// RandNumber 生成指定位数随机数字字符串
func RandNumber(n int) string {
	var letters = []rune("1234567890")
	rand.Seed(time.Now().UnixNano())
	b := make([]rune, n)
	for i := range b {
		b[i] = letters[rand.Intn(len(letters))]
	}
	return string(b)
}

// NewPasswordByMd5 生成一个md5 加盐 的加密结果
func NewPasswordByMd5(password string) string {
	hash := md5.New()
	hash.Write([]byte(password))
	return fmt.Sprintf("%x", hash.Sum(nil))
}